package Demojava.Demo01;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;

import sun.net.www.content.text.plain;

public class DemoMian {

    public static void showDemoE() {
        DemoE<String> demo = new DemoE<>();
        demo.getInfo("sdfadfaf");
        DemoE<Integer> demoInt = new DemoE<>();
        demoInt.getInfo(100);// what is zhan,first in,last out
    }

    public static void show1() {
        List<String> la = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            la.add("ok" + i);
        }
        la.remove(3);
        la.set(5, "change");
        // System.out.println(la.toString());
        Iterator it = la.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        for (String str : la) {
            System.out.println(str);
        }
    }

    public static void showList() {
        LinkedList<String> link = new LinkedList<>();
        for (int i = 0; i < 5; i++) {
            int tem = 'a' + i;
            char a = (char) tem;
            link.add(a + "");
        }
        show(link);
        link.addFirst("I'm first");
        link.push("push a elsement");
        link.add("add a");
        if (link.contains("a")) {
            link.remove("a");
        }
        link.remove(2);
        link.addLast("add last");
        show(link);
        if (!link.isEmpty()) {
            System.out.println(link.getFirst());
            System.out.println(link.getLast());
        }
    }

    public static void show(List<?> list) {
        System.out.println(list);
    }

    public static void showVector() {
        Vector<String> vec = new Vector<>();
        for (int i = 0; i < 5; i++) {
            int tem = 'a' + i;
            char a = (char) tem;
            vec.add(a + "");
        }
        show(vec);
        System.out.println(vec.elementAt(2));
    }

    public static void showSet() {
        Set<String> set = new HashSet<>();
        for (int i = 0; i < 5; i++) {
            int tem = 'a' + i;
            char a = (char) tem;
            set.add(a + "abcdef");
        }
        set.add("c");
        System.out.println(set);
        Iterator<String> it = set.iterator();
        while (it.hasNext())
            System.out.print(it.next() + " ");
        for (String str : set)
            System.out.print(str + " ");
        System.out.println(set);
    }

    public static void showHash() {
        String a = "重地";
        System.out.println(a.hashCode());
        String b = "通话";
        System.out.println(b.hashCode());
    }

    public static void showLinkedHash() {

    }

    public static void showHash1() {
        Map<String, Integer> map = new HashMap<>();
        map.put("aa", 12);
        map.put("bb", 13);
        map.put("cc", 18);
        map.put("dd", 6);
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println(map.get("aa"));
        for (String str : set) {
            System.out.println(map.get(str) + " ");
        }
    }

    public static void showEntrySet() {
        Map<String, Integer> map = new HashMap<>();
        map.put("aa", 12);
        map.put("bb", 13);
        map.put("cc", 18);
        map.put("dd", 6);
        Set<Map.Entry<String, Integer>> set = map.entrySet();
        Iterator<Map.Entry<String, Integer>> it = set.iterator();
        while (it.hasNext()) {
            Map.Entry<String, Integer> entry = it.next();
            System.out.print(entry.getKey() + entry.getValue() + " ;");
        }

        for (Map.Entry en : set) {
            System.out.print(en.getKey() + ":" + en.getValue() + " ;");
        }
    }

    public static void showDefine() {
        Map<String, DemoPerson> map = new HashMap<>();
        map.put("guangzhou", new DemoPerson("yasuo", 18));
        map.put("wuhan", new DemoPerson("timo", 20));
        map.put("beijing", new DemoPerson("gailun", 28));
        map.put("guangzhou", new DemoPerson("yasuo", 20));

        Set<Map.Entry<String, DemoPerson>> set = map.entrySet();
        for (Map.Entry<String, DemoPerson> en : set) {
            System.out.print(en.getKey() + ":" + en.getValue() + " ");
        }

        Set<String> set1 = map.keySet();
        for (String str : set1) {
            System.out.print(str + ":" + map.get(str) + " ");
        }
    }
    // linkedHashMap extends HashMap double linklist,one is to remmber sort

    public static void StringNumber() {
        Scanner a = new Scanner(System.in);
        String input = a.next();
        Map<String, Integer> map = new HashMap<>();
        char[] arr = input.toCharArray();
        System.out.println(arr);
        for (char ch : arr) {
            // int total = 1;
            if (map.containsKey(ch + "")) {
                Integer tem = map.get(ch + "") + 1;
                map.put(ch + "", tem);
            } else {
                map.put(ch + "", 1);
            }
        }
        System.out.println(map);
    }

    public static void Threadshow() {
        DemoThread a = new DemoThread();
        DemoThread b = new DemoThread();
        a.start();
        b.start();
        Thread c = new Thread() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println("in new Tread");
                System.out.println(this.getName());
            }

        };
        c.setName("I'm C");
        c.start();
        Thread runn = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    System.out.print("runnable" + i + " ");
                }
            }
        });
        runn.start();
    }

    public static void showThreadNew() {
        // 匿名内部类的方式创建线程
        new Thread() {
            HashMap<String, Integer> a = new HashMap<>();

            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println("new Thread " + i + " ");
                }
            }
        }.start();

        Thread aa = new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println("new Runnable " + i + " ");
                }
            }
        });
        aa.start();

    }

    public static void showTh() {
        DemoThread b = new DemoThread();
        Thread th = new Thread(b);
        Thread th1 = new Thread(b);
        Thread th2 = new Thread(b);
        th.start();
        th1.start();
        th2.start();

    }

    public static void showFile() throws IOException {
        // String pathSeparator = File.pathSeparator;
        // System.out.println(pathSeparator);
        // String separator = File.separator;
        // System.out.println(separator);
        File f1 = new File("E:\\myGit\\qinjieGit\\Demojava\\Demo01\\test");
        File file = new File("E:\\myGit\\qinjieGit\\Demojava\\Demo01");
        // System.out.println(file);
        // System.out.println(file.getAbsolutePath());
        // System.out.println(file.getName());
        // System.out.println(file.getPath());
        // System.out.println(file.length());
        // System.out.println(file.createNewFile());
        // System.out.println(f1.createNewFile());
        // System.out.println(f1.mkdirs());
        // System.out.println(f1.delete());
        String[] arr = file.list();
        File[] arrf = file.listFiles();
        for (String a : arr)
            System.out.println(a);
        for (File a : arrf)
            System.out.println(a);
    }

    public static int allAdd(int a) {
        if (a == 1)
            return 1;
        else
            return a * allAdd(a - 1);
    }

    public static void printAllFile(File file) {
        File[] list = file.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                if (file.isDirectory())
                    return true;
                else {
                    if (file.getName().toLowerCase().endsWith("java"))
                        return true;
                }
                return false;
            }
        });
        for (File a : list) {
            if (a.isDirectory()) {
                printAllFile(a);
            } else
                System.out.println(a);
        }
    }

    public static void printAllFileLambda(File file) {
        File[] list = file.listFiles((File d, String name) -> {
            return new File(d, name).isDirectory() || name.toLowerCase().endsWith("java");
        });
        for (File a : list) {
            if (a.isDirectory()) {
                printAllFile(a);
            } else
                System.out.println(a);
        }
    }

    public static void fileInput() {
        try {
            FileReader fr = new FileReader("E:\\myGit\\qinjieGit\\Demojava\\Demo01\\1.txt");
            int len = 0;
            char[] arr = new char[512];
            while ((len = fr.read(arr)) != -1) {
                System.out.println(len);
                System.out.println(new String(arr, 0, len));
            }
            // 出现中文乱码
            // while((len = fr.read())!=-1){
            // System.out.print(len+" ");
            // System.out.println((char)len);
            // }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void fileOutput() {
        try {

            // int len = 0;
            // char[] arr = new char[512];
            // while((len = fr.write(arr))!=-1){
            // System.out.println(len);
            // System.out.println(new String(arr,0,len));
            // }
            FileWriter fr = new FileWriter("E:\\myGit\\qinjieGit\\Demojava\\Demo01\\b.txt");
            fr.write("I love China");
            fr.flush();
            fr.close();

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void showPropertiesStore() {
        Properties pl = new Properties();
        pl.setProperty("qin", "24");
        pl.setProperty("zhang", "23");
        pl.setProperty("datoger", "0");
        Set set = pl.keySet();
        try {
            FileWriter fw = new FileWriter("E:\\myGit\\qinjieGit\\Demojava\\Demo01\\c.txt");
            pl.store(fw, "Properties store");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

        /// System.out.println(set);
    }

    public static void showPropertiesLoad() {
        Properties pl = new Properties();
        try {
            pl.load(new FileReader("E:\\myGit\\qinjieGit\\Demojava\\Demo01\\c.txt"));
            Set<String> set = pl.stringPropertyNames();
            for(String arr : set){
                System.out.println(arr+" " + pl.getProperty(arr));
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.getMessage());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
    public static void Bufferfos(){
        FileOutputStream fos;
        try {
            fos = new FileOutputStream("E:\\myGit\\qinjieGit\\Demojava\\Demo01\\d.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            for (int i = 65; i < 72; i++) {
                bos.write(i);
            }
            bos.close();
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    public static void main(String[] args)  {
        Bufferfos();
        System.exit(0);     
    }

}